
package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.joysuch.wwyt.datadump.biz.model.MhMajorHazardChemProductRelBean;
import com.joysuch.wwyt.mh.entity.MhMajorHazardChemProductRel;
import com.joysuch.wwyt.mh.entity.dsl.QMhMajorHazardChemProductRel;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询重大危险源关联的危化品
 */
@Component
public class ExportMhMajorHazardRelChemProductConvertor implements FieldValueConvert<Long, List<MhMajorHazardChemProductRelBean>> {

    private static final Logger logger = LoggerFactory.getLogger(ExportMhMajorHazardRelChemProductConvertor.class);

    @Autowired
    private JPAQueryFactory factory;

    @Override
    public List<MhMajorHazardChemProductRelBean> convert(Object bean, Long value, DataDumpField dataField) {
        QMhMajorHazardChemProductRel qr = QMhMajorHazardChemProductRel.mhMajorHazardChemProductRel;
        List<MhMajorHazardChemProductRel> list = factory.select(qr).from(qr).where(qr.majorHazardId.eq(value)).fetch();

        return list.stream().map(r -> {
            MhMajorHazardChemProductRelBean b = new MhMajorHazardChemProductRelBean();
            BeanUtils.copyProperties(r, b);
            b.setCasNo(r.getMhMajorHazardChemProduct().getCasNo());
            b.setChemProductName(r.getMhMajorHazardChemProduct().getName());
            return b;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean supportBatch() {
        return true;
    }

    @Override
    public List<List<MhMajorHazardChemProductRelBean>> convert(List<Object> bean, List<Long> value, DataDumpField dataDumpField) {
        if(value == null || value.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        QMhMajorHazardChemProductRel qr = QMhMajorHazardChemProductRel.mhMajorHazardChemProductRel;
        List<MhMajorHazardChemProductRel> list = factory.select(qr).from(qr).where(qr.majorHazardId.in(value)).fetch();
        MultiValueMap<Long,MhMajorHazardChemProductRelBean> map = new LinkedMultiValueMap<>();
        list.forEach(r -> {
            MhMajorHazardChemProductRelBean b = new MhMajorHazardChemProductRelBean();
            BeanUtils.copyProperties(r, b);
            b.setCasNo(r.getMhMajorHazardChemProduct().getCasNo());
            b.setChemProductName(r.getMhMajorHazardChemProduct().getName());
            map.add(r.getMajorHazardId(), b);
        });
        List<List<MhMajorHazardChemProductRelBean>> result = new ArrayList<>();
        for(Long s : value) {
            // 按照ID顺序排列结果
            result.add(map.get(s));
        }
        return result;
    }
}
